import { setGlobal } from '@swda/utils'

//-----------------------------------------------------------------------------
// 枫之舞状态窗口 - 基本定义
//-----------------------------------------------------------------------------
function SWDA_Window_Status() {
    this.initialize.apply(this, arguments);
}

SWDA_Window_Status.prototype = Object.create(Window_Selectable.prototype);
SWDA_Window_Status.prototype.constructor = SWDA_Window_Status;

SWDA_Window_Status.prototype.initialize = function () {
    const width = Graphics.boxWidth + 48;
    const height = Graphics.boxHeight + 48;
    Window_Selectable.prototype.initialize.call(this, -24, -24, width, height);
    this._actor = null;
    this.reservePortraitImages();
    this.refresh();
    this.activate();
};

SWDA_Window_Status.prototype.setActor = function (actor) {
    if (this._actor !== actor) {
        this._actor = actor;
        this.refresh();
    }
};

SWDA_Window_Status.prototype.refresh = function () {
    this.contents.clear();
    if (this._actor) {
        const firstLineY = 16;
        const lineHeight = this.lineHeight();
        this.drawBlock1();								//肖像与名字
        this.drawBlock2(firstLineY + lineHeight * 2);	//基本信息（命仙体，经验）
        this.drawBlock3(firstLineY + lineHeight * 7);	//基本参数与装备
    }
};

//肖像与名字
SWDA_Window_Status.prototype.drawBlock1 = function () {
    this.SWDA_drawActorPortrait(this._actor, 0, 0);
    this.drawActorName(this._actor, 36, 32, 64);
};

//基本信息（命仙体，经验）
SWDA_Window_Status.prototype.drawBlock2 = function (y) {
    this.drawBasicInfo(840, y + this.lineHeight());
    this.drawLevelInfo(490, y + this.lineHeight());
    this.drawExpInfo(490, y + this.lineHeight() * 2);
    this.drawStateIcons(490, y + this.lineHeight() * 3);
};

//基本参数与装备
SWDA_Window_Status.prototype.drawBlock3 = function (y) {
    this.drawEquipments(490, y);
    this.drawParameters(840, y);
    //this.drawElementInfo(1030, y);
};


SWDA_Window_Status.prototype.drawBasicInfo = function (x, y) {
    const lineHeight = this.lineHeight();
    this.setSmallerFont();
    this.changeTextColor(this.systemColor());
    this.drawText(TextManager.hp, x, y + lineHeight * 0, 60);
    this.drawActorHp(this._actor, x + 84 + 22, y + lineHeight * 0, 150);

    this.setSmallerFont();
    this.changeTextColor(this.systemColor());
    this.drawText(TextManager.mp, x, y + lineHeight * 1, 60);
    this.drawActorMp(this._actor, x + 84 + 22, y + lineHeight * 1, 150);

    this.setSmallerFont();
    this.changeTextColor(this.systemColor());
    this.drawText(TextManager.tp, x, y + lineHeight * 2, 60);
    this.drawActorTp(this._actor, x + 84 + 22, y + lineHeight * 2, 150);
    this.resetFontSettings();
};





//-----------------------------------------------------------------------------
// 绘制角色数值
//-----------------------------------------------------------------------------
SWDA_Window_Status.prototype.drawParameters = function (x, y) {
    const lineHeight = this.lineHeight();
    const textWidth = 84;
    const xNum = x + textWidth + 14;
    const numWidth = 44;
    let y2 = 0;
    //Param
    for (let i = 0; i < 5; i++) {
        const paramId = i + 2;
        const y2 = y + lineHeight * i;
        this.setSmallerFont();
        this.changeTextColor(this.systemColor());
        this.drawText(TextManager.param(paramId), x, y2, textWidth);
        this.resetTextColor();
        this.drawText(this._actor.param(paramId), xNum, y2, numWidth, 'right');
        this.resetFontSettings();
    }

    /*
    //Xparam
    y2 += lineHeight;
    this.changeTextColor(this.systemColor());
    this.drawText(TextManager.param(8), x, y2, textWidth);
    this.resetTextColor();
    this.setNumFontSize();
    this.drawText(Math.round(this._actor.hit*100)+'%', xNum, y2, numWidth, 'right');
    this.resetFontSettings();
	
    y2 += lineHeight;
    this.changeTextColor(this.systemColor());
    this.drawText(TextManager.param(9), x, y2, textWidth);
    this.resetTextColor();
    this.setNumFontSize();
    this.drawText(Math.round(this._actor.eva*100)+'%', xNum, y2, numWidth, 'right');
    this.resetFontSettings();
	
    y2 += lineHeight;
    this.changeTextColor(this.systemColor());
    this.drawText('暴击率', x, y2, textWidth);
    this.resetTextColor();
    this.setNumFontSize();
    this.drawText(Math.round(this._actor.cri*100)+'%', xNum, y2, numWidth, 'right');
    this.resetFontSettings();
    */
};

//-----------------------------------------------------------------------------
// 绘制等级、经验
//-----------------------------------------------------------------------------
// 绘制等级
SWDA_Window_Status.prototype.drawLevelInfo = function (x, y) {
    this.setSmallerFont();
    this.changeTextColor(this.systemColor());
    this.drawText(TextManager.level, x, y, 268);
    this.resetTextColor();
    this.drawActorLevel(this._actor, x + 268 - 60, y);
    this.resetFontSettings();
};

// 绘制经验
SWDA_Window_Status.prototype.drawExpInfo = function (x, y) {
    const expText = TextManager.exp;
    let value1 = this._actor.currentExp();
    let value2 = this._actor.nextLevelExp();
    if (this._actor.isMaxLevel()) {
        value1 = '-------';
        value2 = '-------';
    }
    // 绘制经验槽
    const color1 = this.expGaugeColor1();
    const color2 = this.expGaugeColor2();
    if (this._actor.isMaxLevel()) {
        this.drawGauge(x + 84, y, 268 - 84, 1, color1, color2);
    } else {
        this.drawGauge(x + 84, y, 268 - 84, value1 / value2, color1, color2);
    }
    this.setSmallerFont();
    this.changeTextColor(this.systemColor());
    this.drawText(expText, x, y, 268);
    this.resetTextColor();
    this.drawText((value1 + '/' + value2), x, y, 268, 'right');
    this.resetFontSettings();

};

// 绘制状态
SWDA_Window_Status.prototype.drawStateIcons = function (x, y) {
    this.setSmallerFont();
    this.changeTextColor(this.systemColor());
    this.drawText(TextManager.status, x, y);
    this.resetTextColor();
    if (this._actor.allIcons().length === 0) {
        this.drawText('健康', x + 84, y, 268 - 84);
    } else {
        this.drawActorIcons(this._actor, x + 84, y, 32 * 6);
    }
};

//-----------------------------------------------------------------------------
// 绘制装备
//-----------------------------------------------------------------------------
SWDA_Window_Status.prototype.drawEquipments = function (x, y) {
    const equips = this._actor.equips();
    const count = Math.min(equips.length, this.maxEquipmentLines());
    this.setSlotNameWidth(this._actor);
    for (let i = 0; i < count; i++) {
        this.drawEquipmentItem(i, x, y + this.lineHeight() * i, 300);
    }
};

SWDA_Window_Status.prototype.slotName = function (index) {
    const slots = this._actor.equipSlots();
    return this._actor ? $dataSystem.equipTypes[slots[index]] : '';
};
SWDA_Window_Status.prototype.setSlotNameWidth = function (actor) {
    if (!actor) return;
    this._nameWidth = 0;
    for (let i = 0; i < actor.equipSlots().length; ++i) {
        const text = $dataSystem.equipTypes[actor.equipSlots()[i]] + ' ';
        this._nameWidth = Math.max(this._nameWidth, this.textWidth(text));
    }
};
// 仿YEP_EquipCore

SWDA_Window_Status.prototype.drawEmptySlot = function (wx, wy, ww) {
    this.changePaintOpacity(false);
    const ibw = Window_Base._iconWidth + 4;
    this.resetTextColor();
    this.drawIcon(Yanfly.Icon.EmptyEquip, wx + 2, wy + 2);
    const text = Yanfly.Param.EquipEmptyText;
    this.drawText(text, wx + ibw, wy, ww - ibw);
};


// 装备槽（状态界面）
SWDA_Window_Status.prototype.drawEquipmentItem = function (index, x, y, width) {
    Window_EquipSlot.prototype.drawItem.call
    if (!this._actor) return;
    width = width || 300;
    this.setSmallerFont();
    this.changeTextColor(this.systemColor());
    const ww1 = this._nameWidth;
    const iconBoxWidth = Window_Base._iconWidth + 4;
    this.drawText(this.slotName(index), x, y, ww1);
    const ww2 = width - ww1;
    const item = this._actor.equips()[index];
    if (item) {
        // 画法宝经验槽
        let itemObject = new Game_Item();
        itemObject.setObject(item);
        if (itemObject.isMagicWeapon()) {
            this.drawItemExp(itemObject, x + ww1 + iconBoxWidth, y + 2, 150);
        }
        this.setSmallerFont();
        this.drawItemName(item, x + ww1, y, ww2);
    } else {
        this.drawEmptySlot(x + ww1, y, ww2);
    }
    this.changePaintOpacity(true);

};



// 装备槽（装备界面）
SWDA_Window_Status.Window_EquipSlot_drawItem = Window_EquipSlot.prototype.drawItem;
Window_EquipSlot.prototype.drawItem = function (index) {
    const rect = this.itemRectForText(index);
    const item = this._actor.equips()[index];
    const ww1 = this._nameWidth;
    const iconBoxWidth = Window_Base._iconWidth + 4;
    if (item) {
        // 画法宝经验槽
        let itemObject = new Game_Item();
        itemObject.setObject(item);
        if (itemObject.isMagicWeapon()) {
            this.drawItemExp(itemObject, rect.x + ww1 + iconBoxWidth, rect.y + 2, 150);
        }
    }
    SWDA_Window_Status.Window_EquipSlot_drawItem.call(this, index);
};

// 装备物品（装备界面）
SWDA_Window_Status.Window_EquipItem_drawItem = Window_EquipItem.prototype.drawItem;
Window_EquipItem.prototype.drawItem = function (index) {
    const item = this._data[index];
    if (item === null) {
        this.drawRemoveEquip(index);
    } else {
        const rect = this.itemRectForText(index);
        const iconBoxWidth = Window_Base._iconWidth + 4;
        if (item) {
            // 画法宝经验槽
            let itemObject = new Game_Item();
            itemObject.setObject(item);
            if (itemObject.isMagicWeapon()) {
                this.drawItemExp(itemObject, rect.x + iconBoxWidth, rect.y + 2, 150);
            }
        }
        SWDA_Window_Status.Window_EquipItem_drawItem.call(this, index);
    }
};

SWDA_Window_Status.prototype.maxEquipmentLines = function () {
    return 9;
};

SWDA_Window_Status.prototype.drawProfile = function (x, y) {
    this.drawTextEx(this._actor.profile(), x, y);
};


//-----------------------------------------------------------------------------
// SWDA：修改使用枫之舞状态界面
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Scene_Status
//
// The scene class of the status screen.

Scene_Status.prototype.create = function () {
    Scene_MenuBase.prototype.create.call(this);

    this._statusWindow = new SWDA_Window_Status();
    this._statusWindow.setHandler('cancel', this.popScene.bind(this));
    this._statusWindow.setHandler('pagedown', this.nextActor.bind(this));
    this._statusWindow.setHandler('pageup', this.previousActor.bind(this));
    //this._statusWindow.reservePortraitImages();
    this.addWindow(this._statusWindow);

    this.createActorPageWindow();

};

Scene_Status.prototype.start = function () {
    Scene_MenuBase.prototype.start.call(this);
    this.refreshActor();
};

// 角色翻页
Scene_Status.prototype.createActorPageWindow = function() {
    this._actorPageWindow = new Window_ActorPage();
    this._actorPageWindow.activate();
    this._actorPageWindow.setScene(this);
    this.addWindow(this._actorPageWindow);
};

//-----------------------------------------------------------------------------
// SWDA：修改或新增的关于绘制状态信息的部分
//-----------------------------------------------------------------------------

SWDA_Window_Status.prototype.drawActorName = function (actor, x, y, width) {
    width = width || 168;
    if (!this._nameBgSprite){
      this._nameBgSprite = new Sprite_Base();
      this._nameBgSprite.bitmap = ImageManager.loadBitmap('img/system/UI/', 'MenuBg_Name');
      this._nameBgSprite.x = -this.x + x;
      this._nameBgSprite.y = -this.y;
      this.addChildAt(this._nameBgSprite,0);
    }
    this.setTitleFont();
    this.changeTextColor(this.textColor(0));
    this.contents.fontBold = true;
    this.drawVerticalText(actor.name(), x, y, width, this.titleFontSize(), 'center')
    this.resetFontSettings();
  };
  
//-----------------------------------------------------------------------------
// SWDA：绘制属性抗性
//-----------------------------------------------------------------------------
/*
SWDA_Window_Status.prototype.drawElementInfo = function (x, y,) {
    const dx = x || 0;
    let dy = y || 0;
    const infoArray = [1, 2, 3];
    for (let i = 0; i < infoArray.length; ++i) {
        this.setSmallerFont();
        this.drawElementName(infoArray[i], dx, dy);
        this.drawElementData(this._actor, infoArray[i], dx + 48, dy);
        this.resetFontSettings();
        dy += this.lineHeight();
    }
};
*/
setGlobal('SWDA_Window_Status', SWDA_Window_Status)